package utils

import (
	"regexp"
	"strings"

	"github.com/zmqge/vireo-gin-admin/config"
)

func BuildFileURL(storagePath string) string {
	var urlBuilder strings.Builder

	// 1. 拼接基础域名和路径
	urlBuilder.WriteString(config.App.FileUpload.Domain)
	urlBuilder.WriteString(config.App.FileUpload.BaseURL)
	urlBuilder.WriteString(config.App.FileUpload.BasePath)

	// 2. 处理存储路径中的斜杠
	// 先替换反斜杠为正斜杠
	cleanedPath := strings.ReplaceAll(storagePath, "\\", "/")
	// 再替换连续斜杠为单斜杠
	cleanedPath = regexp.MustCompile(`/+`).ReplaceAllString(cleanedPath, "/")
	urlBuilder.WriteString(cleanedPath)

	return urlBuilder.String()
}
